package com.zhihu_lvliangi_android.option;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

import org.junit.Test;

import android.R.integer;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.file.cache.CacheUtil;
import com.android.utils.ImageUtil;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lidroid.xutils.BitmapUtils;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.option.home.HomePageFragment;
import com.zhihu_lvliangi_android.option.parent.ParentActivity;
import com.zhihu_lvliangi_android.service.DataFormat;
import com.zhihu_lvliangi_android.service.MobileApplication;
import com.zhihu_lvliangi_android.service.TaskID;
import com.zhihu_lvliangi_android.util.Constant;
import com.zhihu_lvliangi_android.util.RequestURL;

import dalvik.annotation.TestTarget;

public class HomePageActivity extends ParentActivity {

	private String WEATHER_CACHEKEY = "Weather_CacheKey";
	private ImageView mWeatherLogoView;
	private TextView mTemperatureTextView,mWeatherTextView;
	private BitmapUtils mBitMapUtils;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mBitMapUtils = new BitmapUtils(this);
		super.onCreate(savedInstanceState);
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, HomePageFragment.newInstance()).commit();
		}
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_home_page;
	}

	@Override
	protected void setupViews() {
		initActionBar();
	}

	@SuppressWarnings("deprecation")
	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		View view = LayoutInflater.from(this).inflate(
				R.layout.homepage_actionbar, null);
		actionBar.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.homepage_actionbar_bg)));
		// 使自定义的普通View能在title栏显示, actionBar.setCustomView能起作用.
		actionBar.setDisplayShowHomeEnabled(false);//不显示app图标
		actionBar.setDisplayShowCustomEnabled(true); 
		actionBar.setDisplayShowCustomEnabled(true); 
		actionBar.setCustomView(view);
		 mWeatherLogoView=  (ImageView) view.findViewById(R.id.actionbar_weath_logo);
		 mTemperatureTextView = (TextView) view.findViewById(R.id.temperature);
		 mWeatherTextView = (TextView) view.findViewById(R.id.weatherinfo);
		setWeatherFromCache();
	}



	@Override
	protected void initialized() {

	}

	@Override
	protected void threadTask() {
		MobileApplication.getInstance().clientTask.executeGet(
				TaskID.TASK_WEATHER_INFO, DataFormat.JSONOBJECT,
				RequestURL.getWeatherUrl("吕梁"), this);
	}

	@Override
	public void onResponse(Object response, Enum<?> reqId) {
		super.onResponse(response, reqId);
		switch ((TaskID)reqId) {
		case TASK_WEATHER_INFO:
			String str = new JsonParser().parse(response.toString()).getAsJsonObject().get("results").getAsJsonArray().get(0).getAsJsonObject()
			.get("weather_data").getAsJsonArray().get(0).toString();//当天天气情况
			parseWeatherJson(str);
			break;

		default:
			break;
		}
		
		
	}

	private void parseWeatherJson(String str) {
		if (MobileApplication.getInstance().isSDCardMounted()) {
			CacheUtil.get(new File(Constant.SD_CACHE_PATH)).put(WEATHER_CACHEKEY, str);
		}
		JsonObject jsonObject = new JsonParser().parse(str).getAsJsonObject();
		String temperature = jsonObject.get("temperature").toString();
		String weatherInfo = jsonObject.get("weather").toString();
		String dayUrl = jsonObject.get("dayPictureUrl").toString();
		String nightUrl = jsonObject.get("nightPictureUrl").toString();
		resetWeather(temperature,weatherInfo,dayUrl,nightUrl);
	}

	private void setWeatherFromCache()  {
		String str = null;
		if (MobileApplication.getInstance().isSDCardMounted()) {
			str=CacheUtil.get(new File(Constant.SD_CACHE_PATH)).getAsString(WEATHER_CACHEKEY);
		}
		if (str!=null) {
			parseWeatherJson(str);
		}	else {
			resetWeather("暂无数据","暂无数据",null,null);
		}
	}
	private void resetWeather(String temperature, String weatherInfo,
			String dayUrl, String nightUrl) {
		//加入缓存
		
		String imgUrl =nightUrl;
		boolean get;
		if (getDatePm()) {
			imgUrl = dayUrl;
		}
		mTemperatureTextView.setText(temperature.replaceAll("\"", ""));
		mWeatherTextView.setText(weatherInfo.replaceAll("\"", ""));
		if (!TextUtils.isEmpty(imgUrl)) {
			mBitMapUtils.display(mWeatherLogoView, imgUrl.replaceAll("\"", ""),MobileApplication.getInstance().initBitMapConfig(R.drawable.weather_log_sun, R.drawable.weather_log_sun));
		}
	}

	public boolean getDatePm() {
		int hour_24 = Calendar.getInstance(Locale.PRC)
				.get(Calendar.HOUR_OF_DAY);
		System.out.println("hour_24 =" + hour_24);

		if (hour_24 > 6 && hour_24 < 18) {
			return true;
		} else {
			return false;
		}
	}
	
	
}
